package com.twb.auth.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.twb.auth.entity.twbApi.TwbApi;
import com.twb.auth.entity.twbApi.dto.SearchTwbApiDto;
import com.twb.auth.entity.twbApi.vo.SaveTwbApiVo;
import com.twb.auth.entity.twbApi.vo.SearchTwbApiVo;
import com.twb.auth.entity.twbApi.vo.UpdateTwbApiByIdVo;
import com.twb.auth.mapper.TwbApiMapper;
import com.twb.auth.service.ITwbApiService;
import com.twb.core.base.entity.TwbPageInfo;
import com.twb.core.base.service.impl.BaseService;
import com.twb.core.exception.ServiceException;
import com.twb.core.util.BeanUtils;
import com.twb.core.util.ExcelUtils;
import com.twb.core.util.TwbPageUtil;
import com.twb.system.util.ExcelDataDicHandlerImpl;

/** 
 * @ClassName TwbApiService 
 * @Description API信息service
 
 * @author DSY
 * @date 2021-05-13 13:43:43
 */ 
@Service("twbApiService")
public class TwbApiService extends BaseService<TwbApiMapper,TwbApi> implements ITwbApiService {

	@Autowired
	TwbApiMapper twbApiMapper;

	@Override
	public TwbPageInfo<SearchTwbApiDto> searchTwbApi(SearchTwbApiVo vo) {
		TwbPageUtil.startPage(vo);
		List<SearchTwbApiDto> list = twbApiMapper.searchTwbApi(vo);
		return TwbPageUtil.of(list);
	}
	
	@Override
	public List<SearchTwbApiDto> searchTwbApiList(SearchTwbApiVo vo) {
		TwbPageUtil.orderBy(vo.getSorts());
		List<SearchTwbApiDto> list = twbApiMapper.searchTwbApi(vo);
		return list;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean saveTwbApi(SaveTwbApiVo vo) {
		
		TwbApi checkVo = new TwbApi();
		checkVo.setId(vo.getId());
		checkVo.setIsdel(0);
		long count = this.count(checkVo);
		if(count > 0) {
			throw new ServiceException(String.format("[%s]已存在", checkVo.getName()));
		}
	
		TwbApi record = new TwbApi();
		BeanUtils.copyNotNullProperties(vo, record);
		record.setId(vo.getId());
		this.setCreateInfo(record);
		this.setUpdateInfo(record);
		return this.save(record);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateTwbApiById(UpdateTwbApiByIdVo vo) {
		TwbApi record = twbApiMapper.selectById(vo.getId());
		if(null == record) {
			throw new ServiceException("数据不存在");
		}
		
		if (!vo.getName().equalsIgnoreCase(record.getName())) {
			TwbApi checkVo = new TwbApi();
			checkVo.setName(vo.getName());	//此处需要修改为需要校验重复的字段
			checkVo.setIsdel(0);
			long count = this.count(checkVo);
			if(count > 0) {
				throw new ServiceException(String.format("[%s]已存在", checkVo.getName()));
			}
		}
		
		BeanUtils.copyProperties(vo, record);
		this.setUpdateInfo(record);
		return this.updateById(record);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteTwbApiById(String id) {
		return this.removeById(id);
	}
	
	@Override
	public boolean deleteTwbApiByIds(String ids) {
		
		return this.removeByIds(Arrays.asList(ids.split(",")));
	}

	@Override
	public int logicBatchDeleteTwbApiByIds(String ids) {
		List<String> list = new ArrayList<String>();
		Collections.addAll(list, ids.split(","));
		return twbApiMapper.logicBatchDeleteByIds(list);
	}
	
	@Override
	public void exportExcel(SearchTwbApiVo vo, HttpServletResponse response) throws Exception {
		TwbPageInfo<SearchTwbApiDto> pageInfo = this.searchTwbApi(vo);
		ExcelDataDicHandlerImpl excelDataDicHandlerImpl = new ExcelDataDicHandlerImpl();
		ExcelUtils.exportExcel(vo,"API信息", pageInfo.getList(), SearchTwbApiDto.class, response, excelDataDicHandlerImpl);
	}

	@Override
	public int importExcel(Map<String, String> map) {
		System.out.println(map.toString());
		return 0;
	}

	@Override
	public List<SearchTwbApiDto> listApiIdsByResourceId(String resourceId) {
		
		return twbApiMapper.listApiIdsByResourceId(resourceId);
	}
}
